<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="微信openid" prop="unionId">
      <el-input v-model="dataForm.unionId" placeholder="微信openid"></el-input>
    </el-form-item>
    <el-form-item label="微信unionid" prop="openId">
      <el-input v-model="dataForm.openId" placeholder="微信unionid"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别 1 女，2 男" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别 1 女，2 男"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="用户签名" prop="sign">
      <el-input v-model="dataForm.sign" placeholder="用户签名"></el-input>
    </el-form-item>
    <el-form-item label="是否禁用 1（true）已禁用，  0（false）未禁用" prop="isDisabled">
      <el-input v-model="dataForm.isDisabled" placeholder="是否禁用 1（true）已禁用，  0（false）未禁用"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除 1（true）已删除， 0（false）未删除" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="逻辑删除 1（true）已删除， 0（false）未删除"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getNowFormatDate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          unionId: '',
          openId: '',
          mobile: '',
          nickname: '',
          sex: '',
          age: '',
          avatar: '',
          sign: '',
          isDisabled: '',
          isDeleted: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          unionId: [
            { required: true, message: '微信openid不能为空', trigger: 'blur' }
          ],
          openId: [
            { required: true, message: '微信unionid不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别 1 女，2 男不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          sign: [
            { required: true, message: '用户签名不能为空', trigger: 'blur' }
          ],
          isDisabled: [
            { required: true, message: '是否禁用 1（true）已禁用，  0（false）未禁用不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '逻辑删除 1（true）已删除， 0（false）未删除不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/educustomer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.unionId = data.eduCustomer.unionId
                this.dataForm.openId = data.eduCustomer.openId
                this.dataForm.mobile = data.eduCustomer.mobile
                this.dataForm.nickname = data.eduCustomer.nickname
                this.dataForm.sex = data.eduCustomer.sex
                this.dataForm.age = data.eduCustomer.age
                this.dataForm.avatar = data.eduCustomer.avatar
                this.dataForm.sign = data.eduCustomer.sign
                this.dataForm.isDisabled = data.eduCustomer.isDisabled
                this.dataForm.isDeleted = data.eduCustomer.isDeleted
                this.dataForm.gmtCreate = data.eduCustomer.gmtCreate
                this.dataForm.gmtModified = data.eduCustomer.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/educustomer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'unionId': this.dataForm.unionId,
                'openId': this.dataForm.openId,
                'mobile': this.dataForm.mobile,
                'nickname': this.dataForm.nickname,
                'sex': this.dataForm.sex,
                'age': this.dataForm.age,
                'avatar': this.dataForm.avatar,
                'sign': this.dataForm.sign,
                'isDisabled': this.dataForm.isDisabled,
              //   'isDeleted': this.dataForm.isDeleted,
              //   'gmtCreate': this.dataForm.gmtCreate,
              //   'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
